package org.example.memora.DTO;

import lombok.Data;
import org.example.memora.entity.Post;
import java.time.ZoneOffset;
import java.time.LocalDateTime;

@Data
public class PostDTO extends Post {

    // 用于存储计算后的综合推荐分数
    private Double score;

    // 构造方法：通过 Post 类构造 PostDTO
    public PostDTO(Post post) {
        super();  // 调用父类 Post 的构造方法
        this.setPostId(post.getPostId());
        this.setUserId(post.getUserId());
        this.setTitle(post.getTitle());
        this.setContent(post.getContent());
        this.setCoverUrl(post.getCoverUrl());
        this.setMediaUrls(post.getMediaUrls());
        this.setSummary(post.getSummary());
        this.setCategoryId(post.getCategoryId());
        this.setStatus(post.getStatus());
        this.setNoteType(post.getNoteType());
        this.setLikeCount(post.getLikeCount());
        this.setCollectionCount(post.getCollectionCount());
        this.setCommentCount(post.getCommentCount());
        this.setViewCount(post.getViewCount());
        this.setPinned(post.getPinned());
        this.setCount(post.getCount());
        this.setCreatedAt(post.getCreatedAt());
        this.setUpdatedAt(post.getUpdatedAt());
        this.setIsDeleted(post.getIsDeleted());
    }


    public void calculateScore(double heatWeight, double freshnessWeight) {
        // 获取当前时间和帖子创建时间的差值，计算新鲜度分数
        long timeDiff = System.currentTimeMillis() - this.getCreatedAt().toEpochSecond(ZoneOffset.UTC) * 1000;  // 毫秒
        double freshnessScore = 1.0 / (1 + timeDiff / (1000 * 60 * 60 * 24));  // 按天计算新鲜度，越新鲜分数越高

        // 计算热度分数：综合 viewCount 和 likeCount
        double heatScore = this.getViewCount() * 0.6 + this.getLikeCount() * 0.4;

        // 计算综合分数 = 热度分数 * 权重 + 新鲜度分数 * 权重
        this.setScore(heatScore * heatWeight + freshnessScore * freshnessWeight);
    }


}


